package com.k7.mobile.platform.web.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.k7.mobile.platform.bo.IAndroidpowerBO;
import com.k7.mobile.platform.bo.IGramBO;
import com.k7.mobile.platform.bo.IGramimageBO;
import com.k7.mobile.platform.bo.IKeywordBO;
import com.k7.mobile.platform.bo.ISearchQueryBO;
import com.k7.mobile.platform.bo.ITypeBO;
import com.k7.mobile.platform.dataobject.AndroidpowerDO;
import com.k7.mobile.platform.dataobject.GramDO;
import com.k7.mobile.platform.dataobject.GramimageDO;
import com.k7.mobile.platform.dataobject.KeywordDO;
import com.k7.mobile.platform.dataobject.QueryDO;
import com.k7.mobile.platform.dataobject.RankingDO;
import com.k7.mobile.platform.dataobject.RecommendxDO;
import com.k7.mobile.platform.dataobject.TypeDO;
import com.k7.mobile.platform.web.support.Constants;

public class ApiAction extends BaseAction{
	private static final long serialVersionUID = -5048373149842002950L;
	
	private IGramBO gramBO;
	private ITypeBO typeBO;
	private ISearchQueryBO searchQueryBO;
	private IGramimageBO gramimageBO;
	private IAndroidpowerBO androidpowerBO;
	private IKeywordBO keywordBO;
	private Map jsonMap;	
	private Integer page;
	private Integer parentId;
	private Integer typeId;
	private Integer btype;
	private String keyword;
	private Integer pageSize;
	private Long gameId;

	public String recommendList() throws Exception {
		if(page == null)
			page = 1;
		jsonMap = new HashMap();
		RecommendxDO recommendDO = new RecommendxDO();
		recommendDO.setToPage(page);
		recommendDO.setParentId(parentId);
		if(pageSize != null)
			recommendDO.setPerPageSize(pageSize);
		List<GramDO> list = gramBO.findRecommendGameList(recommendDO);
		jsonMap.put("list", list);
		jsonMap.put("pageDO", recommendDO);
		return SUCCESS;
	}
	public String keywordList() throws Exception {
		if(page == null)
			page = 1;
		jsonMap = new HashMap();
		KeywordDO keywordDO = new KeywordDO();
		keywordDO.setToPage(page);
		
		keywordDO.setPerPageSize(Constants.PAGE_SIZE_MAXITEM);
		List<KeywordDO> list = keywordBO.findList(keywordDO);
		jsonMap.put("list", list);
		jsonMap.put("pageDO", keywordDO);
		return SUCCESS;
	}
	public String hotList() throws Exception {
		if(page == null)
			page = 1;
		jsonMap = new HashMap();
		RankingDO rankingDO = new RankingDO();
		rankingDO.setToPage(page);
		if(pageSize != null)
			rankingDO.setPerPageSize(pageSize);
		rankingDO.setTypeId(typeId);
		List<GramDO> list = gramBO.findRankingGameList(rankingDO);
		jsonMap.put("list", list);
		jsonMap.put("pageDO", rankingDO);
		return SUCCESS;
	}
	public String newList() throws Exception {
		if(page == null)
			page = 1;
		jsonMap = new HashMap();
		GramDO gramDO = new GramDO();
		gramDO.setToPage(page);
		if(pageSize != null)
			gramDO.setPerPageSize(pageSize);
		gramDO.setOrderStr("sdate desc");
		List<GramDO> list = gramBO.findList(gramDO);
		jsonMap.put("list", list);
		jsonMap.put("pageDO", gramDO);
		return SUCCESS;
	}
	public String categoryList() throws Exception {
		if(page == null)
			page = 1;
		jsonMap = new HashMap();
		TypeDO typeDO = new TypeDO();
		typeDO.setToPage(page);
		typeDO.setParent(parentId);
		typeDO.setTypeid(Constants.SJ_TYPE_TYPE_ID_CATEGORY);
		typeDO.setPerPageSize(Constants.PAGE_SIZE_MAXITEM);
		List<TypeDO> list = typeBO.findList(typeDO);
		jsonMap.put("list", list);
		jsonMap.put("pageDO", typeDO);
		return SUCCESS;
	}
	
	public String searchList() throws Exception {
		if(page == null)
			page = 1;
		
		QueryDO queryDO = new QueryDO();
		if(pageSize != null)
			queryDO.setPerPageSize(pageSize);
		
		queryDO.setToPage(page);
		queryDO.setKeyword(keyword);
		queryDO.setTypeId(typeId);
		queryDO.setBtype(btype);
		List<GramDO> list = searchQueryBO.searchGame(queryDO);
		jsonMap = new HashMap();
		jsonMap.put("list", list);
		jsonMap.put("pageDO", queryDO);
		return SUCCESS;
	}
	
	public String gameDetail() throws Exception {
		if(gameId == null)return ERROR ;
		GramDO param = new GramDO();
		param.setId(gameId);
		
		GramDO gramDO = gramBO.getGram(param);
		
		List<GramimageDO> imageList = new ArrayList<GramimageDO>();//游戏图片
		List<GramimageDO> videoList = new ArrayList<GramimageDO>();//游戏视频
		
		GramimageDO gramimageDO = new GramimageDO();
		gramimageDO.setGid(gameId);
		gramimageDO.setNeedPaged(Constants.PAGE_NO_NEED_PAGED);
		
		List<GramimageDO> gramimageList = gramimageBO.findList(gramimageDO);
		for (GramimageDO gramimage : gramimageList) {
			if(gramimage.getTypeid() == Constants.GAME_VIDEO){//类型是视频
				videoList.add(gramimage);
			}else{//类型是图片（包括游戏图片、官方图片）
				imageList.add(gramimage);
			}
		}
		List<AndroidpowerDO> androidpowerlist = null;
		try{
			String androidPower = gramDO.getAndroidPower();
			if(!StringUtils.isEmpty(androidPower)){
				AndroidpowerDO androidpowerDO = new AndroidpowerDO();
				List<Integer> powerIds = new ArrayList<Integer>();
				for(String str :androidPower.split(","))
					powerIds.add(Integer.valueOf(str));
				androidpowerDO.setIds(powerIds);
				gramimageDO.setNeedPaged(Constants.PAGE_NO_NEED_PAGED);
				androidpowerlist = androidpowerBO.findList(androidpowerDO);
			}
		}catch(Exception e){
			log.error(e);
		}
		
		jsonMap = new HashMap();
		jsonMap.put("gramDO", gramDO);
		jsonMap.put("imageList", imageList);
		jsonMap.put("videoList", videoList);
		jsonMap.put("androidpowerList", androidpowerlist);
		return SUCCESS;
	}

	public Integer getPage() {
		return page;
	}


	public void setPage(Integer page) {
		this.page = page;
	}



	public Integer getParentId() {
		return parentId;
	}


	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}


	public Map getJsonMap() {
		return jsonMap;
	}


	public void setGramBO(IGramBO gramBO) {
		this.gramBO = gramBO;
	}
	public Integer getTypeId() {
		return typeId;
	}
	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public void setTypeBO(ITypeBO typeBO) {
		this.typeBO = typeBO;
	}
	public void setSearchQueryBO(ISearchQueryBO searchQueryBO) {
		this.searchQueryBO = searchQueryBO;
	}
	public void setKeywordBO(IKeywordBO keywordBO) {
		this.keywordBO = keywordBO;
	}
	public Integer getBtype() {
		return btype;
	}
	public void setBtype(Integer btype) {
		this.btype = btype;
	}
	public Long getGameId() {
		return gameId;
	}
	public void setGameId(Long gameId) {
		this.gameId = gameId;
	}
	public void setGramimageBO(IGramimageBO gramimageBO) {
		this.gramimageBO = gramimageBO;
	}
	public void setAndroidpowerBO(IAndroidpowerBO androidpowerBO) {
		this.androidpowerBO = androidpowerBO;
	}

}
